package com.example.demo.common;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Test08 {

    public static void main(String[] args) {

        List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);

        List<Integer> list2 = loopGetImgAddTagsList(list1, 2, 4);

        System.out.println(JSONObject.toJSONString(list2));
    }

    private static List<Integer> loopGetImgAddTagsList(List<Integer> imgUrls, Integer imgIndexBegin, Integer needNum) {
        if (CollectionUtils.isEmpty(imgUrls)) {
            return null;
        }

        List<Integer> imgUrlList = new ArrayList<>();
        List<Integer> addTagsList = new ArrayList<>();

        int imgIndex = imgIndexBegin;
        for (int i = 0; i < needNum; i++) {
            imgIndex += i;

            if (imgIndex < imgUrls.size()) {
                imgUrlList.add(imgUrls.get(imgIndex));
            } else {
                addTagsList.add(imgUrls.get(imgIndex));
            }
        }

        //添加图片标签
        if (CollectionUtil.isNotEmpty(addTagsList)) {
            for (Integer integer : addTagsList) {
                imgUrlList.add(integer + 10);
            }
        }

        return imgUrlList;
    }
}
